package com.linx.common.core.enums.role;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.linx.common.core.domain.model.RoleEnumBody;
import lombok.Getter;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 系统角色字典
 */
@Getter
public enum SystemRoleEnum {

    入驻企业("resident-enterprise", 10, "入驻企业", new ArrayList<>(){{ add(Arrays.asList("visitor:visitor-apply-invite:list", "meeting-room:meeting-room-reservation:list", "meeting-room:meeting-room-info:list")); }}),
    设施巡检("device_patrol_inspection", 20, "设施巡检", new ArrayList<>(){{ add(Arrays.asList("inspection:inspectio-point:list", "inspection:inspection-plan:list", "inspection:inspection-task:list", "inspection:inspection-report-form:list")); }}),
    安防巡视("device_patrol_inspection", 30, "安防巡视", new ArrayList<>(){{ add(Arrays.asList("camera:patrolRoute:list", "camera:camera-patrol-task:list", "camera:patrolTaskExecutor:list", "camera:patrolTaskInfoLog:list")); }}),
    设施维保("device_patrol_inspection", 40, "设施维保", new ArrayList<>(){{ add(Arrays.asList("maintenance:maintenance-plan:list", "maintenance:maintenance-task:list", "maintenance:maintenance-record:list")); }}),
    ;

    private final String roleKey;

    private final Integer sort;

    private final String roleName;

    private final List<List<String>> permList;

    SystemRoleEnum(String roleKey, Integer sort, String roleName, List<List<String>> permList) {
        this.roleKey = roleKey;
        this.sort = sort;
        this.roleName = roleName;
        this.permList = permList;
    }

    /**
     *  获取枚举列表
     */
    public static List<RoleEnumBody> list() {
        List<RoleEnumBody> enumList = new ArrayList<>();
        Class<SystemRoleEnum> enumClass = SystemRoleEnum.class;

        try {
            Method getRoleKey = enumClass.getMethod("getRoleKey");
            Method getSort = enumClass.getMethod("getSort");
            Method getRoleName = enumClass.getMethod("getRoleName");
            Method getPermList = enumClass.getMethod("getPermList");

            //  通过反射取出Enum所有常量的属性值
            for (SystemRoleEnum enumConstant: enumClass.getEnumConstants()) {
                String roleKey = (String) getRoleKey.invoke(enumConstant);
                Integer sort = (Integer) getSort.invoke(enumConstant);
                String roleName = (String) getRoleName.invoke(enumConstant);
                List<List<String>> permList = JSONArray.parseObject(JSON.toJSONString(getPermList.invoke(enumConstant)), new TypeReference<>() {});

                enumList.add(new RoleEnumBody(roleKey, sort, roleName, permList));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return enumList;
    }

    /**
     * 根据RoleKey获取RoleName
     */
    public static String getRoleNameByRoleKey(String roleKey) {
        for (SystemRoleEnum item: SystemRoleEnum.values()) {
            if (item.getRoleKey().equals(roleKey)) {
                return item.getRoleName();
            }
        }
        return null;
    }

}
